var gradeListViewModel;
var studentListViewModel;
//var gradeListJson = '[{"ceGradeId":"2","gradeTitle":"Grade 2","classList":[{"divisionTitle":"2A","classId":"22A"},{"divisionTitle":"2B","classId":"22B"}]},{"ceGradeId":"3","gradeTitle":"Grade 3","classList":[{"divisionTitle":"3A","classId":"33A"},{"divisionTitle":"3B","classId":"33B"}]}]';
var gradeListJson = '{"grades":[{"ceGradeId":"2","gradeTitle":"Grade 2","classList":[{"divisionTitle":"2A","classId":"22A"},{"divisionTitle":"2B","classId":"22B"}]},{"ceGradeId":"3","gradeTitle":"Grade 3","classList":[{"divisionTitle":"3A","classId":"33A"},{"divisionTitle":"3B","classId":"33B"}]}],	"data":[{"classId":null,"ceGradeId":null,"totalCount":0,"maleCount":0,"femaleCount":0,"isRollNumAssigned":0},{"classId":null,"ceGradeId":null,"totalCount":0,"maleCount":0,"femaleCount":0,"isRollNumAssigned":0}]}';
var url = "";
var academicUrl = "";
var stmgUrl = "";
//var aypId = "acad-7ab69876-cc2e-4399-a066-061823192c94";
var aypId = "acad-7bfaf2ae-9033-4823-9f6c-491a6edcdb0c";
var academicYear = ko.observable();
var sectionId = "sect-782f1539-5f12-4551-8477-74b7bda9daca";
var planStatus = ko.observable();

var divisionsArr = new Array();

var gradeListModel = function() {
	var self = this;

	self.gradeList = ko.observableArray();
	self.selectedGrade = ko.observable();
	self.itemForEditing = ko.observable();
	self.loadStudents = function(item) {
		console.log("loadStudents item:", item, self.selectedGrade());

		self.selectedGrade(item);
		jQuery("#student_promotion_body").empty();

		jQuery("#student_promotion_body").load("/ce-static-testedge/ayp/html/ayp_promoted_student_division_allocation.html", function() {
			console.log("Student Promotion loadStudents Loaded", self.selectedGrade());
			
			ko.applyBindings(gradeListViewModel, jQuery("#student_promotion_body").get(0));
			item.studentList.removeAll();
			ko.utils.arrayForEach(self.selectedGrade().divisions(), function(division) {
				division.studentList.removeAll();
				division.totalCount(0);
				division.femaleCount(0);
				division.maleCount(0);
			});

			if (item.studentList().length == 0) {
				showLoader(true, TYPE_LOADING);
				fetchStudentsByGrade(item.ceGradeId());
			}
		});
	}

	self.addStudent = function(item) {
		console.log("adding student", item, studentsDataViewModel);
		console.log("gradeTitle:", studentsDataViewModel.studentsInfo().grade(), gradeTitle);
		console.log("gradeTitle!!:", studentsDataViewModel.studentsInfo().grade());

		//gradeId = item.ceGradeId();
		//gradeTitle = item.gradeTitle();
		//studentsDataViewModel.studentsInfo().grade(gradeTitle);
		//studentsDataViewModel.studentsInfo().classId(gradeId);

		studentsDataViewModel.studentsInfo().aypId(aypId);
		var vm = ko.validation.group(studentsDataViewModel);
		vm.showAllMessages(false);

		jQuery("#dialog-student").dialog("open");
		jQuery("#dialog-student").dialog('option', 'title', 'Add Student to ' + academicYear());
		jQuery("#tabs").tabs("option", "active", 0);
		getUserBookMark();
	};

	/*self.closeStudentListWindow = function() {
		console.log("-------> closing window");
		studentListViewModel.selectedDivisionItem(null);
		jQuery("#show-students").dialog("close");
	}*/

	self.clickCloseAllWindows = function() { 
		console.log("close window");
		jQuery("#divAllocationDialog").dialog("close");
		jQuery("#conform-dialog").dialog("close");

	}
}

function closeStudentListWindow(event){
	console.log("closing student list window");
	studentListViewModel.selectedDivisionItem(null);
	jQuery("#show-students").dialog("close");
}
var studentListModel = function() {
	var self = this;

	self.selectedDivisionItem = ko.observable();

	/*self.closeStudentListWindow = function() {
		console.log("-------> closing window");
		studentListViewModel.selectedDivisionItem(null);
		jQuery("#show-students").dialog("close");
	}*/
}
function clickCloseAllWindows() {
	console.log("-----> Closing all windows");
	jQuery("#divAllocationDialog").dialog("close");
	jQuery("#conform-dialog").dialog("close");

}

var gradeModel = function() {
	var self = this;

	self.gradeTitle = ko.observable();
	self.ceGradeId = ko.observable();
	self.divisions = ko.observableArray([]);
	self.studentList = ko.observableArray([]);
	self.femaleCount = ko.observable();
	self.maleCount = ko.observable();
	self.totalCount = ko.observable(0);
	self.processType = ko.observable();
	self.orderNo = ko.observable();
	self.isSuffleActive = ko.computed(function() {

		if (self.divisions().length > 0) {
			if (planStatus() == 0) {
				return true;
			} else if (planStatus() == 1 && self.divisions()[0].isRollNumAssigned() == 0) {
				return true
			} else {
				return false
			}
		} else {
			return false;
		}

	})

	self.calculateCount = ko.computed(function() {
		console.log("calculating count", self.studentList().length);
		self.femaleCount(0);
		self.maleCount(0);
		//if (self.studentList().length) {
		//self.totalCount(self.studentList().length);
	//	}

		ko.utils.arrayForEach(self.studentList(), function(student) {
			if (student.gender() == "Male") {
				self.maleCount(self.maleCount() + 1);
			} else {
				self.femaleCount(self.femaleCount() + 1);
			}
		});

		ko.utils.arrayForEach(self.divisions(), function(division) {
			console.log("division.studentList().length:", division.studentList().length, division.totalCount(), division.femaleCount(), division.maleCount());

			if (division.studentList().length > 0) {
				division.studentList.removeAll();
				division.totalCount(0);
				division.femaleCount(0);
				division.maleCount(0);
			} else {
				//	division.femaleCount(0);
				//	division.maleCount(0);
				//	console.log("division.studentList():",division.studentList());
				//division.totalCount(0);
			}

			ko.utils.arrayForEach(self.studentList(), function(student) {
				//console.log("student:", student,student.classId(),division.classId());
				if (student.selectedDivision()) {
					if (division.classId() == student.classId()) {
						console.log("------------> classId");
						if (student.gender() == "Male") {
							division.maleCount(division.maleCount() + 1);
						} else {
							division.femaleCount(division.femaleCount() + 1);
						}

						division.totalCount(division.totalCount() + 1);
						division.studentList.push(student);
					}
				}

			});
			console.log("division count:", division.divisionTitle(), division.femaleCount(), division.maleCount(), division.totalCount());
		});

	})

	self.assignRollNumbersHandler = function() {
		console.log("assigning Roll numbers", self.studentList());
		jQuery("#assign-roll-topband").hide();
		jQuery("#assign-roll").dialog("open");

		//	assignRollNumbers();
	}

	self.assignClassesHandler = function() {
		console.log("--------->Assigning Classes");
		assignClassAndRollNumbers(false)
	}

	self.showStudentList = function(item) {
		console.log("----------> showStudentList", item);
		studentListViewModel.selectedDivisionItem(item);
		self.studentList.removeAll();
	//	if (item.studentList().length == 0) {
			showLoader(true, TYPE_LOADING);
			fetchStudentsByGrade(self.ceGradeId(), sortRollNumberHandler);
	//	} else {
		//	jQuery("#show-students").dialog("open");

		/*	studentListViewModel.selectedDivisionItem().studentList().sort(function(left, right) {
				console.log("left:right", left.rollNumber(), right.rollNumber());
				if (Number(left.rollNumber()) > Number(right.rollNumber())) {
					return 1;
				} else if (Number(left.rollNumber()) < Number(right.rollNumber())) {
					return -1;
				} else {
					return 0;
				}
			});*/
	//	}
	}

	self.showStudentDetails = function(item) {
		console.log("-------> showing StudentDetails", item);
		getStudent(item.userStudentId())
	}
}
function sortRollNumberHandler() {
	console.log("sortRollNumberHandler:");
/*	studentListViewModel.selectedDivisionItem().studentList().sort(function(left, right) {
		console.log("left:right", left.rollNumber(), right.rollNumber());
		if (Number(left.rollNumber()) > Number(right.rollNumber())) {
			return 1;
		} else if (Number(left.rollNumber()) < Number(right.rollNumber())) {
			return -1;
		} else {
			return 0;
		}
	});
*/
	jQuery("#show-students").dialog("open");
}

function getStudent(userStudentId) {
	console.log("----> getStudent====Detailsfrom server");

	jQuery.ajax({
		url : stmgUrl + "getStudent",
		type : "post",
		data : {
			userStudentId : userStudentId
		},
		success : function(result) {
			console.log("Student Data result:", ko.utils.parseJson(result));
			console.log("studentsDataViewModel:", studentsDataViewModel);
			//studentData = ko.utils.parseJson(result);
			//modifyServerData(studentData);
			// = new studentsDataModel(result);
			studentsDataViewModel.update(ko.utils.parseJson(result));
			jQuery("#dialog-student").dialog("open");
			jQuery("#dialog-student").dialog('option', 'title', 'Add Student to ' + academicYear());
			jQuery("#tabs").tabs("option", "active", 0);
		},
		error : function(jqXHR, textStatus, errorThrown) {
			//console.log("error");
		}
	});

}

var divisionModel = function() {
	var self = this;

	self.divisionTitle = ko.observable();
	self.classId = ko.observable();
	self.femaleCount = ko.observable();
	self.maleCount = ko.observable();
	self.totalCount = ko.observable();
	self.gradeCount = ko.observable(0);
	self.isRollNumAssigned = ko.observable(0);
	self.dashBoardSexRatio = ko.observable();
	self.dashBoardTotalCount = ko.observable();
	self.dashBoardStudentList = ko.observableArray();

	self.sexRatio = ko.computed(function() {
		return self.femaleCount() + ":" + self.maleCount();
	});

	self.rollNumberStatus = ko.observable("Assign Roll Number");
	self.studentList = ko.observableArray();

	self.classSelectHandler = function(value, value1) {
		//console.log("------>classSelectHandler", jQuery(value).val());
		console.log("------>classSelectHandler::", value, value1);
	};

}

var studentModel = function() {
	var self = this;

	self.aypId = ko.observable(aypId);
	self.userStudentId = ko.observable();
	self.classId = ko.observable();
	self.previousClassId = ko.observable();
	self.prevClassTitle = ko.observable();
	self.rollNumber = ko.observable("Un Assigned");
	self.firstName = ko.observable();
	self.lastName = ko.observable();
	self.userStudentId = ko.observable();
	self.gender = ko.observable();
	self.divisions = ko.observableArray([]);
	self.selectedDivision = ko.observable();
	self.isInfoModifiedNotSaved = ko.observable(false);
	self.fullName = ko.computed(function() {

		if (self.gender() == "Male") {
			return "Mst. " + self.firstName() + " " + self.lastName();
		} else if (self.gender() == "Female") {
			return "Ms. " + self.firstName() + " " + self.lastName();
		}
	})
	self.selectDivision = ko.computed(function() {
		console.log('-----------> Selected Class');
		var value;
		ko.utils.arrayForEach(self.divisions(), function(item) {
			//console.log("Item:", item.isAssigned(), item.divisionTitle(), item.classId());

			if (self.selectedDivision()) {
				if ((item.isAssigned() == "true") && (self.selectedDivision().classId() != item.classId() )) {
					value = item;
				} else {
					item.isAssigned("false")
				}
			} else {

				if (item.isAssigned() == "true") {
					value = item;
				}
			}
		});

		if (value) {

			self.selectedDivision(value);
			self.classId(value.classId())
			//console.log("self.selectedDivision:", self.selectedDivision());
		}

		return;

	});

	self.divisionChanged = function(item) {
		console.log("--------->divisionChanged", item.isAssigned());
		if (item.isAssigned() == "false") {
			self.isInfoModifiedNotSaved(true)
		}

		return true;
	}
}
var simpleDivisionModel = function() {
	var self = this;

	self.isAssigned = ko.observable('false');
	self.divisionTitle = ko.observable();
	self.classId = ko.observable();
	/*self.calculateCount = ko.computed(function(){

	 var rtnVal = self.isAssignned ed();
	 console.log("rtnVal:",rtnVal,self.divisionTitle(),self.classId() );
	 self.isAssigned("false");

	 })*/

}
function highlightDays() {

	return true;
}

function backtoStudentDashboard() {

	var isNotSaved = false;
	ko.utils.arrayForEach(gradeListViewModel.selectedGrade().studentList(), function(student) {

		if (student.isInfoModifiedNotSaved()) {	
			jQuery("#conform-dialog").dialog("open");
			isNotSaved = true;
		}
	})
	if (!isNotSaved) {
		returnToStudentDashBoard(isNotSaved);
	}

}

function returnToStudentDashBoard(value) {
	console.log("value:", value);
	if (value) {
		console.log("fetching data");
		gradeListViewModel.selectedGrade().studentList.removeAll();
		fetchStudentsByGrade(gradeListViewModel.selectedGrade().ceGradeId());
	}

	jQuery("#conform-dialog").dialog("close");
	jQuery("#student_promotion_body").empty();

	gradeListViewModel.selectedGrade(null);
	//gradeListViewModel.selectedGrade = null;

	jQuery("#student_promotion_body").load("/ce-static-testedge/ayp/html/ayp_promoted_student_dashboard.html", function() {
		//scrollTo('ayp_section_'+active);
		console.log("Student Promotion backtoStudentDashboard");
		ko.applyBindings(gradeListViewModel, jQuery("#student_promotion_body").get(0));
	//	gradeListViewModel  = new gradeListModel();
	//	studentListViewModel = new studentListModel();
	
	//	loadGradesAndDivisions();
		var settings = {
			autoReinitialise : true
		};
		var pane = jQuery('.scroll-pane7')
		pane.jScrollPane(settings);
	});
}

function loadGradesAndDivisions() {
	console.log("---> Loading Grades and Divisions");
	showLoader(true, TYPE_LOADING);
	jQuery.ajax({
		data : {
			sectionId : sectionId
		},
		url : url + "getGradeClassStudentDetails",
		type : "post",
		success : function(result) {
			console.log("----->", result);
			for (var i = 0; i < result.grades.length; i++) {
				var gradeViewModel = new gradeModel();
				console.log("result.grades[i].ceGradeId", result.grades[i].ceGradeId);
				gradeViewModel.ceGradeId(result.grades[i].ceGradeId);
				gradeViewModel.gradeTitle(result.grades[i].gradeTitle);
				gradeViewModel.processType(result.grades[i].processType);
				gradeViewModel.orderNo(result.grades[i].orderNo);
				gradeViewModel.totalCount(result.grades[i].studentCount);
				
				for (var j = 0; j < result.grades[i].divisions.length; j++) {
					//console.log("result.grades[i].divisions[j].divisionTitle:", result.grades[i].divisions[j].divisionTitle);
					var divisionViewModel = new divisionModel();
					divisionViewModel.divisionTitle(result.grades[i].divisions[j].divisionTitle);
					divisionViewModel.classId(result.grades[i].divisions[j].classId);

					gradeViewModel.divisions.push(divisionViewModel);
				}

				gradeListViewModel.gradeList.push(gradeViewModel);
			}

			gradeListViewModel.gradeList.sort(function(item1, item2) {
				//console.log("grade sorting:::", item1, item2);
				if (item1.orderNo() > item2.orderNo()) {
					return 1;
				} else if (item1.orderNo() < item2.orderNo()) {
					return -1;
				} else {
					return 0;
				}

			});
			ko.utils.arrayForEach(gradeListViewModel.gradeList(), function(item) {

				ko.utils.arrayForEach(item.divisions(), function(division) {
					for (var i = 0; i < result.data.length; i++) {
						//console.log("#------------>", result.data[i].ceGradeId, item.ceGradeId());
						//console.log("##------------>", result.data[i].classId, division.classId());
						if ((result.data[i].ceGradeId == item.ceGradeId()) && (result.data[i].classId == division.classId())) {
							//console.log("------------>", division);
							division.femaleCount(result.data[i].femaleCount);
							division.maleCount(result.data[i].maleCount);
							division.totalCount(result.data[i].totalCount);
							division.isRollNumAssigned(result.data[i].isRollNumAssigned);
						//	var totalCount = Number(result.data[i].totalCount) + Number(item.totalCount());
						//	console.log("Total Count:", item.totalCount(), result.data[i].totalCount, totalCount);
							//item.totalCount(totalCount);
						}
					}

					division.dashBoardSexRatio(division.sexRatio());
					division.dashBoardTotalCount(division.totalCount());

					//division.dashBoardStudentList = ko.observable(division.sexRatio());

				});
			});

			console.log("dataFor: ", gradeListViewModel);

			var settings = {
				autoReinitialise : true
			};
			var pane = jQuery('.scroll-pane7')
			pane.jScrollPane(settings);

			showLoader(false);
		},
		error : function(jqXHR, textStatus, errorThrown) {
			console.log("error saveAcademicYearPlan");
			showLoader(false);
		}
	});
}

function fetchStudentsByGrade(_ceGradeId, callBackMethod) {
	console.log("load Students");
	//	gradeListViewModel.studentList.removeAll();
	//	showLoader(true, TYPE_LOADING);
	jQuery.ajax({
		data : {
			aypId : aypId,
			sectionId : sectionId,
			ceGradeId : _ceGradeId
		},
		url : url + "getPromotedStudentsByGrade",
		type : "post",
		success : function(result) {
			console.log("----->fetchStudentsByGrade", result);
			//console.log("dataFor: ", ko.dataFor(this), gradeListViewModel);

			ko.utils.arrayForEach(gradeListViewModel.gradeList(), function(item) {
				for (var i = 0; i < result.length; i++) {
					//if (item.ceGradeId() == result[i].classId) {
					if (item.ceGradeId() == _ceGradeId) {
						var studentViewModel = new studentModel();
						studentViewModel.userStudentId(result[i].userStudentId);
						studentViewModel.firstName(result[i].firstName);
						studentViewModel.lastName(result[i].lastName);
						studentViewModel.gender(result[i].gender);
						studentViewModel.classId(result[i].classId);
						studentViewModel.previousClassId(result[i].previousClassId);
						studentViewModel.prevClassTitle(result[i].prevClassTitle);
						//console.log("result[i].rollNumber:", result[i].rollNumber)
						//var roll_Number = result[i].rollNumber==0?"UnAssigned":result[i].rollNumber;
						studentViewModel.rollNumber(result[i].rollNumber == 0 ? "Un Assigned" : result[i].rollNumber);

						item.studentList.push(studentViewModel);

						var divArr = new Array();
						ko.utils.arrayForEach(item.divisions(), function(division) {
							var simpleDivisionViewModel = new simpleDivisionModel();
							simpleDivisionViewModel.divisionTitle(division.divisionTitle());
							simpleDivisionViewModel.classId(division.classId());

							if (division.classId() == result[i].classId) {
								simpleDivisionViewModel.isAssigned("true");
							} else {
								simpleDivisionViewModel.isAssigned("false");
							}

							divArr.push(simpleDivisionViewModel);
							division.gradeCount(result.length);
						})
						//console.log("div Arr:", divArr);
						studentViewModel.divisions(divArr)
					}

				}

				/*item.studentList().sort(function(left, right) {
					console.log("left:right", left, right);

					if (left.rollNumber() > right.rollNumber()) {
						return 1;
					} else if (left.rollNumber() < right.rollNumber()) {
						return -1;
					} else {
						return 0;
					}

					/*	if (left.selectedDivision().divisionTitle() == right.selectedDivision().divisionTitle())
					 return left.rollNumber() == right.rollNumber() ? 0 : (left.rollNumber() < right.rollNumber() ? -1 : 1)
					 else
					 return left.selectedDivision().divisionTitle() == right.selectedDivision().divisionTitle()? 0 : (left.selectedDivision().divisionTitle() < right.selectedDivision().divisionTitle() ? -1 : 1)
					 
				});*/

			});
			showLoader(false);

			var settings = {
				autoReinitialise : true
			};
			var pane = $('.scroll-pane7')
			pane.jScrollPane(settings);

			if (callBackMethod) {
				console.log("------->callBackMethod");
				callBackMethod();
			}
		},
		error : function(jqXHR, textStatus, errorThrown) {
			console.log("error saveAcademicYearPlan");
			//showLoader(false);
		}
	});
}

function assignClassAndRollNumbers(includeRollNumber) {
	console.log("Saving Roll numbers", includeRollNumber)

	showLoader(true, TYPE_SAVING);

	if (includeRollNumber) {
		var selectedVal = jQuery('input[name=rollNumRdo]:checked').val();

		if (selectedVal == "firstName") {
			gradeListViewModel.selectedGrade().studentList.sort(function(item1, item2) {

				if ((item1.firstName().toLowerCase() + item1.lastName().toLowerCase()) > (item2.firstName().toLowerCase() + item2.lastName().toLowerCase())) {
					return 1;
				} else if ((item1.firstName().toLowerCase() + item1.lastName().toLowerCase()) < (item2.firstName().toLowerCase() + item2.lastName().toLowerCase())) {
					return -1;
				} else {
					return 0;
				}

			});

		} else {
			gradeListViewModel.selectedGrade().studentList.sort(function(item1, item2) {

				if ((item1.lastName().toLowerCase() + item1.firstName().toLowerCase()) > (item2.lastName().toLowerCase() + item2.firstName().toLowerCase())) {
					return 1;
				} else if ((item1.lastName().toLowerCase() + item1.firstName().toLowerCase()) < (item2.lastName().toLowerCase() + item2.firstName().toLowerCase())) {
					return -1;
				} else {
					return 0;
				}

			});
		}
	}

	ko.utils.arrayForEach(gradeListViewModel.selectedGrade().divisions(), function(division) {
		var count = 1;
		ko.utils.arrayForEach(division.studentList(), function(student) {
			if (includeRollNumber) {
				student.rollNumber(count++);
			} else {
				student.rollNumber(0);
			}
			student.isInfoModifiedNotSaved(false);
		});

		division.dashBoardSexRatio(division.sexRatio());
		division.dashBoardTotalCount(division.totalCount());

	})
	var mapping = {
		'ignore' : ["firstName", "lastName", "gender", "divisions", "selectedDivision", "fullName", "isInfoModifiedNotSaved", "processType", "isSuffleActive","prevClassTitle","previousClassId"]
	}
	var jsonData = ko.mapping.toJSON(gradeListViewModel.selectedGrade().studentList(), mapping);
	var processType;

	processType = includeRollNumber == false ? 2 : 3;

	console.log("JSOn Data:", jsonData, processType);

	jQuery.ajax({
		data : {
			savedata : jsonData,
			processType : processType

		},
		url : url + "processStudentPromote",
		type : "post",
		success : function(result) {
			console.log("----->assignClassAndRollNumbers", result);
			//alert("Students allocated to Divisions");
			if (processType == 2) {
				jQuery("#assign-roll-topband").show();
				jQuery("#assign-roll").dialog("open");
			} else if (processType == 3) {
				jQuery("#divAllocationDialog").dialog("open");
			}
			gradeListViewModel.selectedGrade().processType(processType);
			if (includeRollNumber) {
				ko.utils.arrayForEach(gradeListViewModel.selectedGrade().divisions(), function(division) {
					division.isRollNumAssigned(1);

				});
			}
			showLoader(false);
		},
		error : function(jqXHR, textStatus, errorThrown) {
			console.log("error saveAcademicYearPlan");
			showLoader(false);
		}
	});

}

function saveClassDetails() {
	console.log("---------->Saving Class Details");

}

ko.bindingHandlers.customJScrollBinding = {
	init : function(element, valueAccessor) {
		// This will be called when the binding is first applied to an element
		// Set up any initial state, event handlers, etc. here

		//  jQuery(element).button();
		//var pane = jQuery('.scroll-pane7');
		//pane.jScrollPane('destroy');
		console.log("init");
		var settings = {
			autoReinitialise : true
		};
		jQuery(element).jScrollPane(settings);
	},
	update : function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
		// This will be called once when the binding is first applied to an element,
		// and again whenever the associated observable changes value.
		// Update the DOM element based on the supplied values here.
		console.log("update");
		var settings = {
			autoReinitialise : true
		};
		jQuery(element).jScrollPane(settings);
	}
};

function retrunToAcademicYear(event) {
	console.log("returning to Academic Year");
	location.href = academicUrl.replace("p_p_state=exclusive", "p_p_state=normal") + "getAcademics";
}

jQuery(function() {
	console.log("Page Loaded : ayp-student-promotion");
	//url = jQuery('#baseUrl').val().replace("DUMMY", "");
	//aypId = jQuery('#aypId').val();
	gradeListViewModel = new gradeListModel();
	studentListViewModel = new studentListModel();
	console.log("studentListViewModel:", studentListViewModel);
	if (jQuery('#baseUrl').val()) {
		url = jQuery('#baseUrl').val().replace("DUMMY", "");
		stmgUrl = jQuery('#stmgUrl').val().replace("DUMMY", "");
		academicUrl = jQuery('#academicUrl').val().replace("DUMMY", "");

		console.log("-------> URL:", url, stmgUrl);
		aypId = jQuery('#aypId').val();
		sectionId = jQuery('#sectionId').val();
		planStatus(jQuery('#planStatus').val());
		console.log("Plan Status:", jQuery('#planStatus').val());

		console.log("BaseUrl:", url, aypId, sectionId);
	} else {
		//aypId = "acad-29248e35-97fc-4c0b-98a0-c7f806825633";
		url = "http://p1392.tatainteractive.com:8080/group/hinpubsc-abd/my-classes?p_p_id=AYPManagement_WAR_ceportaltestedge&p_p_lifecycle=0&p_p_state=exclusive&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1&_AYPManagement_WAR_ceportaltestedge_Operation=";
	}
	///ce-static-testedge/ayp/html/

	jQuery("#student_promotion_body").load("/ce-static-testedge/ayp/html/ayp_promoted_student_dashboard.html", function() {
		//scrollTo('ayp_section_'+active);

		ko.applyBindings(gradeListViewModel, document.getElementById("student_promotion_body"));
		loadGradesAndDivisions();

		console.log("Student Promotion Dashboard Loaded");
	});

	ko.applyBindings(studentListViewModel, document.getElementById("show-students"));

	jQuery("#show-students").dialog({
		autoOpen : false,
		width : 600,
		minHeight : 100,
		modal : true
	});

	jQuery("#divAllocationDialog").dialog({
		autoOpen : false,
		width : 300,
		minHeight : 200,
		modal : true
	});

	jQuery("#conform-dialog").dialog({
		autoOpen : false,
		width : 300,
		minHeight : 200,
		modal : true
	});

	//jQuery("#shifts").buttonset();

	//	var btn;
	//	btn = jQuery('#rad_buttons span')[0];
	//	btn.click();

})

